<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<script type="text/javascript">
	$(function() {
		var resourceTrees = JSON.parse('${resouceTrees}');
		$('#resourceIds').tree({
			data : resourceTrees,
			parentField : 'pid',
			value:[1],
			checkbox:true,
			lines : true,
			//panelHeight : 'auto',
			check:true,
			value:"",
			onLoadSuccess : function() {
				parent.$.messager.progress('close');				
				try{
					var idstr = '${role.resourceIds }';
					var ids = idstr.split(',');
					for(var key in ids){
						var node = $('#resourceIds').tree('find',ids[key]);
						if(node){
							var cn = $('#resourceIds').tree('getChildren',node.target);
							if(!cn || cn.length == 0){
								$('#resourceIds').tree('check',node.target);
							}
						}
					}
					
					
				} catch(e){}
			}
		});

		var getPtreeId = function(node,nodeMap){
			var pnode = $('#resourceIds').tree('getParent',node.target);
			if(pnode){
					nodeMap[pnode.id] = '1';
					getPtreeId(pnode,nodeMap);
			}
		};
		
		$('#form').form({
			url : '/roleController/edit',
			onSubmit : function() {
				parent.$.messager.progress({
					title : '提示',
					text : '数据处理中，请稍后....'
				});
				var isValid = $(this).form('validate');
				var nodes = $('#resourceIds').tree('getChecked');
				if(nodes){
					var nodeMap = {};
					var nodestrs = '';
					for(var key in nodes){
						nodeMap[nodes[key].id] = '1';
					 	 getPtreeId(nodes[key],nodeMap);
					}
					for(var k in nodeMap){
						if(nodestrs != ''){
							nodestrs += ',';
						} 
						nodestrs += k;
					}
					$('#form input[name="resourceIds"]').val(nodestrs);
				}
				
				if (!isValid) {
					parent.$.messager.progress('close');
				}
				return isValid;
			},
			success : function(result) {
				parent.$.messager.progress('close');
				result = $.parseJSON(result);
				if (result.success) {
					parent.$.modalDialog.openner_treeGrid.treegrid('reload');//之所以能在这里调用到parent.$.modalDialog.openner_treeGrid这个对象，是因为role.jsp页面预定义好了
					parent.$.modalDialog.handler.dialog('close');
				} else {
					parent.$.messager.alert("提示",result.msg, "info");
				}
			}
		});
	});
</script>
<div class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'center',border:false" title="" style="overflow: hidden;width: 100%;height: 100%">
		<form id="form" method="post" style="width: 100%;height: 100%">
			<input value="${role.id }" name="id" type="hidden"/>
			<table class="table table-hover table-condensed"  style="width: 100%;height: 100%">
				<tr>
				<th>选择小区</th>
						<td>
						<input name="residenceId"  value="${role.residenceId } " type="hidden"/>
						<input name="residencName" value="${ role.residenceName}" style="width:390px"   readonly="readonly"/>
						</td>
				</tr>
				<tr>
					<th>上级角色</th>
					<td>
							<input value="${role.parentId }" name="parentId" type="hidden"/>
							<input value="${role.parentName }" name="parentName" style="width:390px"  type="text" readonly="readonly"/>
				</td>
				</tr>
				<tr>
					<th>角色名称</th>
					<td><input name="name" type="text" placeholder="请输入角色名称" value="${role.name }" style="width:390px"  class="easyui-validatebox span2" data-options="required:true"  /></td>
				</tr>
				<tr>
					<th>排序</th>
					<td>	<input class="easyui-slider"  name="sortWeight" value="${role.sortWeight }" style="width:390px" data-options="min:1,max:100,showTip:true" />
				</tr>
				<tr>
					<th>备注</th>
					<td colspan=""><textarea name="remark" style="width:390px"  rows="" cols="" class="span5">${role.remark }</textarea></td>
				</tr>
				<tr style="height: 200px">
					<th>分配资源</th>
					<td style="overflow: auto;">
					<div id="resourceIds" style="height: 100%;width: 100%;overflow: auto;" ></div>
					<input type="hidden" value="" name="resourceIds"  value="${role.resourceIds }"/>
					</td>
				</tr>
			</table>
		</form>
	</div>
</div>